/**
*  
* @Function 公共目录首页 
* @ProjectName credit-data-platform 
* @PackageName com.py.credit.controller    
* @FileName ContentStatisticsController.java    
* @Date 2018年9月20日上午10:54:13
* @Author Min DongXv
* @Copyright (c) 2018, 金电联行 All Rights Reserved.   
*   
*/  
package com.py.credit.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.service.ContentStatisticsService;

/**
 * 
 * @Function 公共目录首页
 * @ClassName ContentStatisticsController
 * @Date 2018年9月20日 上午10:54:13
 * @Author Min DongXv
 * 
 */
@RestController
@RequestMapping(value = "/fShareContent")
public class ContentStatisticsController extends BaseController {

	/**
	 * 有5条限制
	 */
	private static final String HAS_LIMIT = "Y";

	@Autowired
	private ContentStatisticsService contentStatisticsService;

	/**
	 * 
	 * @api {get} /fShareContent/contentStatistics.json 目录统计接口
	 * @apiName contentStatistics
	 * @apiVersion 0.0.1
	 * @apiGroup 政务外网-公共目录
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {List} contentStatistics 公共目录统计
	 * @apiSuccess {List} deptName 部门名称
	 * @apiSuccess {List} tableNum 表数量
	 * @apiSuccess {List} classifyStatistics 信用主体分类统计
	 * @apiSuccess {List} kName 库名称
	 * @apiSuccess {List} tableNum 表数量
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":{"contentStatistics":[{"deptName":"绍兴市发改委","tableNum":1}],"classifyStatistics":[{"tableNum":0,"kName":"个人"}]}}
	 */
	@RequestMapping(value = "/contentStatistics.json")
	public RestServiceResult<Map<String, Object>> contentStatistics() {
		RestServiceResult<Map<String, Object>> result = new RestServiceResult<>();
		Map<String, Object> data = new HashMap<String, Object>(2);
		try {
			List<Map<String, Object>> classifyStatistics = contentStatisticsService.classifyStatistics();
			List<Map<String, Object>> contentStatistics = contentStatisticsService.contentStatistics(HAS_LIMIT);
			data.put("classifyStatistics", classifyStatistics);
			data.put("contentStatistics", contentStatistics);
			result.setData(data);
			return result;
		} catch (Exception e) {
			ExceptionLogger.error("ContentStatisticsController#contentStatistics", "查询目录统计异常！ERROR:" + e.getMessage(),
					e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("查询目录统计异常！");
			return result;
		}
	}

	/**
	 * 
	 * @api {get} /fShareContent/classifyStatistics.json 信用主体分类统计
	 * @apiName classifyStatistics
	 * @apiVersion 0.0.1
	 * @apiGroup 政务外网-公共目录
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {List} classifyStatistics 信用主体分类统计
	 * @apiSuccess {List} kName 库名称
	 * @apiSuccess {List} tableNum 表数量
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[{"tableNum":127,"kName":"企业"}]}
	 */
	@RequestMapping(value = "/classifyStatistics.json")
	public RestServiceResult<List<Map<String, Object>>> classifyStatistics() {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<>();
		try {
			List<Map<String, Object>> data = contentStatisticsService.classifyStatistics();
			result.setData(data);
			return result;
		} catch (Exception e) {
			ExceptionLogger.error("ContentStatisticsController#classifyStatistics", "信用主体统计异常！ERROR:" + e.getMessage(),
					e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("信用主体统计异常！");
			return result;
		}
	}

	/**
	 * 
	 * @api {get} /fShareContent/pubContentInfos.json 公共目录数据统计
	 * @apiName pubContentInfos
	 * @apiVersion 0.0.1
	 * @apiGroup 政务外网-公共目录
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {String} deptName 部门名称
	 * @apiSuccess {Integer} deptId 部门ID
	 * @apiSuccess {Integer} tableNum 表数量
	 * @apiSuccess {Integer} dataNum 数据数量
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[{"deptName":"工商部门","deptId":1,"tableNum":14,"dataNum":0}]}
	 */
	@RequestMapping(value = "/pubContentInfos.json")
	public RestServiceResult<Map<String, Object>> pubContentInfos() {
		RestServiceResult<Map<String, Object>> result = new RestServiceResult<>();
		Map<String, Object> dataResult = new HashMap<String, Object>(2);
		Map<String, Integer> totalNum = new HashMap<>(2);
		int deptNum = 0;
		int num = 0;
		totalNum.put("deptNum", 0);
		totalNum.put("num", 0);
		try {
			List<Map<String, Object>> data = contentStatisticsService.pubContentInfos();
			if (!CollectionUtils.isEmpty(data)) {
				for (Map<String, Object> m : data) {
					if (!CollectionUtils.isEmpty(m)) {
						num = num + Integer.parseInt(String.valueOf(m.get("sjnum")));
					}
				}
				deptNum = data.size();
			}
			dataResult.put("deptNum", data);
			totalNum.put("deptNum", deptNum);
			totalNum.put("num", num);
			dataResult.put("totalNum", totalNum);
			result.setData(dataResult);
			return result;
		} catch (Exception e) {
			ExceptionLogger.error("ContentStatisticsController#pubContentInfos", "查询公共目录异常！ERROR:" + e.getMessage(), e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("查询公共目录异常！");
			return result;
		}
	}

	/**
	 * 
	 * @api {get} /fShareContent/pubContentDetail.json 查询公共目录详情
	 * @apiName pubContentDetail
	 * @apiVersion 0.0.1
	 * @apiGroup 政务外网-公共目录
	 * @apiParam {Integer} deptId 部门ID
	 * @apiParamExample {json} 请求参数示例
	 *                  {"deptId":1}
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {Integer} kId 库ID
	 * @apiSuccess {Integer} dataNum 数据数量
	 * @apiSuccess {Integer} id 表ID
	 * @apiSuccess {String} creditTime 更新时间
	 * @apiSuccess {String} tableName 表名
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":[{"kId":100000,"dataNum":0,"id":210000,"creditTime":"--","tableName":"法人行政许可"}]}
	 */
	@RequestMapping(value = "/pubContentDetail.json")
	public RestServiceResult<List<Map<String, Object>>> pubContentDetail(
			@RequestParam(value = "deptId", defaultValue = "", required = true) Integer deptId) {
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<>();
		try {
			List<Map<String, Object>> data = contentStatisticsService.pubContentDetail(deptId);
			result.setData(data);
			return result;
		} catch (Exception e) {
			ExceptionLogger.error("ContentStatisticsController#pubContentDetail", "查询公共目录详情异常！ERROR:" + e.getMessage(),
					e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("查询公共目录详情异常！");
			return result;
		}
	}

	/**
	 * 
	 * @api {get} /fShareContent/allContentStatistics.json 目录统计
	 * @apiName allContentStatistics
	 * @apiVersion 0.0.1
	 * @apiGroup 政务外网-公共目录
	 * @apiSuccess {String} code 状态码:1 成功 -1 失败
	 * @apiSuccess {String} msg 错误信息
	 * @apiSuccess {int} count 结果集数据量
	 * @apiSuccess {String} data 结果集
	 * @apiSuccess {List} pubContent 信用信息目录
	 * @apiSuccess {String} kName 库名称
	 * @apiSuccess {String} tableNum 表数量
	 * @apiSuccess {List} subContent 订阅信用信息目录
	 * @apiSuccess {String} kName 库名称
	 * @apiSuccess {String} tableNum 表数量
	 * @apiSuccess {List} content 各部门公共目录统计
	 * @apiSuccess {String} deptName 部门名称
	 * @apiSuccess {String} tableNum 表数量
	 * @apiSuccess {Map} contentType 公开范围目录统计 P:社会公开,S:政务共享,A:授权查询
	 * @apiSuccess {Map} contentFrequency 更新频率目录统计 S:实时，D:每日,W:每周,M:每月,Q:每季度,Y:每年
	 * @apiSuccessExample {json} 成功返回结果
	 *                    {"code":0,"msg":null,"count":0,"data":{"contentFrequency":{"Q":1,"Y":3,"S":2,"D":105,"M":11,"W":2},"contentType":{"P":27,"A":97,"S":0},"subContent":[{"tableNum":124,"kName":"企业"}],"content":[{"deptName":"工商部门","tableNum":14}],"pubContent":[{"tableNum":124,"kName":"企业"}]}}
	 */
	@RequestMapping(value = "/allContentStatistics.json")
	public RestServiceResult<Map<String, Object>> allContentStatistics() {
		RestServiceResult<Map<String, Object>> result = new RestServiceResult<>();
		try {
			Map<String, Object> data = contentStatisticsService.allContentStatistics();
			result.setData(data);
			return result;
		} catch (Exception e) {
			ExceptionLogger.error("ContentStatisticsController#allContentStatistics", "目录统计异常！ERROR:" + e.getMessage(),
					e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("目录统计异常！");
			return result;
		}
	}
}
